const path = require("path");
const webpack = require("webpack");
const merge = require('webpack-merge');
const isDebug = process.env.NODE_ENV === 'development';

const base = require('./webpack.config.base');

module.exports = merge(base,{
    mode: isDebug ? 'development' : 'production',
    entry: {
        index: [
            //热加载局部更新
            "react-hot-loader/patch",
            //配置本地入口
            "webpack-dev-server/client?http://0.0.0.0:3000",
            "webpack/hot/only-dev-server",
            "./src/index"
        ],
        //无论entry是否引用都会将此模块和webpack运行文件进行打包
        vendor: [
            "react",
            //"mobx",
            //"mobx-react",
            "react-dom",
            "react-router",
            //"whatwg-fetch"
        ]
    },
    output: {
        path: path.join(__dirname, "dist"),
        publicPath: "/",
        filename: "app.[hash].js",
        chunkFilename: "[name].chunk.js",
    },
    devServer: {
        hot: true,
        contentBase: path.resolve(__dirname, "dist"),
        port: 3000,
        //用于配置DevServer服务器监听的地址。如果你想要局域网中其他设备访问你本地的服务，
        //可以在启动的时候带上--host 0.0.0.0.host的默认值是127.0.0.1即只有本地可以访问DevServer的HTTP服务
        //host: "0.0.0.0",
        publicPath: "/",
        historyApiFallback: true,
        //关闭后可以接受来自任何 HOST 的请求
        disableHostCheck: true,
        //
        clientLogLevel: 'none'
    },
    devtool: "cheap-eval-source-map",
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    "style-loader",
                    "css-loader",
                    "postcss-loader?sourceMap",
                    "resolve-url-loader",
                ]
            },
        ]
    },
    optimization: {
        runtimeChunk: {
            name: 'manifest'
        },
        //minimizer:false, // [new UglifyJsPlugin({...})]
        splitChunks: {
            chunks: 'initial',
            cacheGroups: {
                vendor: {
                    test: /node_modules\/(.*)\.js/,
                    name: 'vendor',
                    chunks: 'initial',
                    minSize: 30000,
                    minChunks: 1,
                    priority:10,
                    reuseExistingChunk: false
                }
            }
        }
    },
    plugins: [
        //热加载局部更新
        new webpack.HotModuleReplacementPlugin()
        //new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /nb/)
    ]
});